﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2012/3/12 19:18:18
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Restaurant.EntityType;
using Sector7.Framework.Core;
using System.Data.SqlClient;
using Sector7.Restaurant.DAO.Utility;
using Sector7.Framework.DB;

namespace Sector7.Restaurant.DAO.OleDB
{
    /// <summary>
    /// <!-- The description about this class. -->
    /// </summary>
    public class MetadataDAO : IMetadataDAO
    {
        #region Public functions
        public int Create(RS_Metadata metadata)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql = @"
                    INSERT INTO RS_Metadata
                            (TypeId
                            ,Name
                            ,Keyword
                            ,IsValid)
                        VALUES
                            (@TypeId
                            ,@Name
                            ,@Keyword
                            ,@IsValid)
                    SELECT @@IDENTITY";

                return Convert.ToInt32(opt.ExecuteScalar(
                    sql,
                    DBUtil.CreateSqlParameter(metadata.TypeId, "TypeId"),
                    DBUtil.CreateSqlParameter(metadata.Name, "Name"),
                    DBUtil.CreateSqlParameter(metadata.Keyword, "Keyword"),
                    DBUtil.CreateSqlParameter(true, "IsValid")));
            }
        }

        public List<RS_Metadata> ReadAll(int typeId)
        {
            using (DBHelper opt = new DBHelper())
            {
                List<RS_Metadata> metadatas = new List<RS_Metadata>();
                List<SqlParameter> sqlParameters = new List<SqlParameter>();
                string sql = @"
                    SELECT *
                        FROM RS_Metadata";
                if (typeId > 0)
                {
                    sql += @"
                        WHERE TypeId = @TypeId
                        ORDER BY TypeId";
                    sqlParameters.Add(DBUtil.CreateSqlParameter(typeId, "TypeId"));
                }

                SqlDataReader reader = opt.ExecuteQuery(sql, sqlParameters.ToArray());

                while (reader.Read())
                {
                    object metadata = new RS_Metadata();

                    DBUtil.ReadData(reader, ref metadata);

                    metadatas.Add(metadata as RS_Metadata);
                }

                return metadatas;
            }
        }

        public List<RS_Metadata> ReadAllForUsage(int usage)
        {
            using (DBHelper opt = new DBHelper())
            {
                List<RS_Metadata> metadatas = new List<RS_Metadata>();

                string sql = @"
                    SELECT *
                        FROM
                            RS_Metadata AS metadata
                        LEFT JOIN
                            RS_MetadataType AS metadataType ON metadata.TypeId = metadataType.TypeId
                        WHERE
                            (Usage & @Usage) = @Usage";

                SqlDataReader reader = opt.ExecuteQuery(
                    sql,
                    DBUtil.CreateSqlParameter(usage, "Usage"));

                while (reader.Read())
                {
                    object metadata = new RS_Metadata();

                    DBUtil.ReadData(reader, ref metadata);

                    metadatas.Add(metadata as RS_Metadata);
                }

                return metadatas;
            }
        }

        public int Update(EntityDelta delta)
        {
            using (DBHelper opt = new DBHelper())
            {
                return opt.ExecuteNonQuery(
                    DBUtil.CreateUpdateSql(delta),
                    DBUtil.CreateUpdateParameters(delta));
            }
        }

        public DBOptResultType Delete(int metadataId)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql =
                    "DELETE FROM RS_Metadata    " +
                    "   WHERE Id = @Id          ";

                return (DBOptResultType)opt.ExecuteNonQuery(
                    sql,
                    DBUtil.CreateSqlParameter(metadataId, "Id"));
            }
        }
        #endregion
    }
}
